<?php
defined('_XR_EXEC') or die();
/**
 * 逻辑处理类 
 */
class cart_Model extends Model
{ 
	
	/* 刷新购物车，将无效的数据去除掉，并且返回购物车信息 */
	public function renewGetCart($user_id){
		$db = factory::getDBO();
		$db->begin();
		$nowdate = date('Y-m-d H:i:s');
		$rs = $db->loadassoclist('
		select 
		a.*,
		b.name as combo_name,
		b.type as combo_type,
		b.price as combo_price,
		b.min_num as combo_min_num,
		b.min_price as combo_min_price
		from t_cart a
		left join t_combo b on a.combo_id=b.id
		 where a.user_id="'.$user_id.'" and a.deleted=0 order by a.id asc ');
		foreach($rs as &$v){
			if($db->loadresult('select count(a.id) from `t_combo` a where id="'.$v['combo_id'].'" and  a.deleted=0 and a.publishs=1  and  a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ') <= 0 ){
				//删除操作
				$db->update('cart',array(
					'id'=>$v['id'],
					'deleted'=>'1'
				));
			}
			$v['cart_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price as vege_price from t_cart_item a left join t_vege b on a.vege_id=b.id where a.cart_id="'.$v['id'].'"  ');
			$v['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price as vege_price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$v['combo_id'].'"  ');
			
			# 更新价格
			if($v['combo_type']=='0'){
				//固定套餐，重新根据套餐定价计算金额
				$v['price'] = $v['combo_price'];
				$v['price_total'] = $v['nums'] * $v['price'];
			}else{
				//组合套餐，通过选菜重新计算金额
				$v['price'] = 0;
				if(count($v['cart_item'])>0){
					foreach($v['cart_item'] as $val){
						$v['price'] += $val['vege_price'] * $val['vege_num'];
					}
				}
				$v['price_total'] = $v['nums'] * $v['price'];
			}
			$db->update('cart',array(
				'id'=>$v['id'],
				'price'=>$v['price'],
				'price_total'=>$v['price_total']
			));
		}
		
		# 重新获取
		$rs = $db->loadassoclist('
		select 
		a.*,
		b.name as combo_name,
		b.type as combo_type ,
		b.price as combo_price,
		b.min_num as combo_min_num,
		b.min_price as combo_min_price,
		b.sendbdate,
		b.sendedate
		from t_cart a
		left join t_combo b on a.combo_id=b.id
		 where a.user_id="'.$user_id.'" and a.deleted=0 order by a.id asc ');
		foreach($rs as &$val){
			$val['cart_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price as vege_price from t_cart_item a left join t_vege b on a.vege_id=b.id where a.cart_id="'.$val['id'].'"  ');
			$val['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price as vege_price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$val['combo_id'].'"  ');
		}
		$db->commit();
		return $rs;
	}
	
	/* 更新购物车 */
	public function updatenum($user_id,$cart_id,$nums){
		$nums = intval($nums);
		if($nums <= 0){
			return $this->setError('购物车数量设置错误');
		}
		$nowdate = date('Y-m-d H:i:s');
		$db = factory::getDBO();
		$db->begin();
		$rs = $db->loadassoc('select * from t_cart where id="'.$cart_id.'" and user_id="'.$user_id.'" and deleted=0 ');
		if(!$rs){
			return true;
		}
		$rs_combo = $db->loadassoc('select a.* from `t_combo` a where a.id="'.$rs['combo_id'].'" and  a.deleted=0  and a.publishs=1  and  a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ');
		if(!$rs_combo){
			//删除购物车
			$db->update('cart',array(
				'id'=>$rs['id'],
				'deleted'=>'1'
			));
			$db->commit();
			return true;
		}
		$db->update('cart',array(
			'id'=>$rs['id'],
			'nums'=>$nums,
			'price_total'=>($nums*$rs['price'])
		));
		$db->commit();
		return true;
	}
	
	/* 得到用户购物车数量 */
	public function getCartNums($user_id){
		$db = factory::getDBO();
		$rs = intval($db->loadresult('select sum(nums) from t_cart where user_id="'.$user_id.'" and deleted=0 '));
		return $rs;
	}
}

?>